/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gen2phen.labs.actions;

import java.util.ArrayList;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.gen2phen.labs.domain.Disease;
import org.gen2phen.labs.domain.Drug;
import org.gen2phen.labs.services.Loader;

/**
 *
 * @author pedrolopes
 *
 * GEN2PHEN Semantic Web Labs: Knowledge Netowork
 *
 */
@UrlBinding("/disease/{$event}/{query}")
public class DiseaseActionBean implements ActionBean {

    private ActionBeanContext context;
    private String query;
    private ArrayList<Drug> drugs = new ArrayList<Drug>();
    private Disease disease;

    public Disease getDisease() {
        return disease;
    }

    public void setDisease(Disease disease) {
        this.disease = disease;
    }

    public ArrayList<Drug> getDrugs() {
        return drugs;
    }

    public void setDrugs(ArrayList<Drug> drugs) {
        this.drugs = drugs;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public ActionBeanContext getContext() {
        return context;
    }

    /**
     * Main handler will get Disease information and associated Drug list.
     *
     * @return Text string with all information ready to be processed by client-side JavaScript
     */
    @DefaultHandler
    public Resolution get() {
        String html = "";

        disease = Loader.getDiseases().get(query);
        // add disease info to result string
        html += disease.getName() + "#" + disease.getOmim() + "#" + disease.getDiseaseClass() + "$";

        for (Drug d : disease.drugs.values()) {
            html += d.getName() + "%%"; // add drug name to result string
        }

        return new StreamingResolution("text", html.substring(0, html.length() - 2));
    }
}
